package com.asset.filter;

import com.asset.constants.AppConstant;
import com.asset.pojo.Url;
import com.asset.pojo.User;
import com.asset.service.UrlService;
import com.asset.service.UserService;
import com.asset.service.imp.UrlServiceImp;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class BackendFilter implements Filter {

	protected UrlService urlService;

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

		String webName = request.getContextPath();// 当前被包含的war中真正的webName
//		request.setCharacterEncoding("UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		ServletContext sc = request.getServletContext();
//		XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);
//
//		if(cxt != null && cxt.getBean("urlServiceImp") != null && urlService == null){
//			urlService = (UrlService) cxt.getBean("urlServiceImp");
//		}
//		String uri = request.getRequestURI();
//		List<Url> urlList = urlService.listUrl();
//		int flag = 0;
//		HttpSession session = request.getSession();
//		if(uri.contains("css")||uri.contains("js")||uri.contains("ajax")||uri.contains("jpg")){
//			//如果是css或者js文件直接给过
//		}else{
//			if(uri.contains(".action")){
//				uri = uri.replace(".action","");
//			}
//			for(int i= 0; i<urlList.size();i++){
//				if(uri.equals(urlList.get(i).getUrl())){
//					flag =1;
//				}
//			}
//			//如果页面不存在
//			if(flag == 0){
//				User user = (User)session.getAttribute(AppConstant.LOGIN_USER);
//				if(user == null){
//					response.sendRedirect(AppConstant.TO_LOGIN);
//					return;
//				}else{
//					response.sendRedirect("/notFind");
//					return;
//				}
//			}
//		}
//        String colId = request.getParameter("colId");
//		if (colId != null&&!"".equals(colId)) {
//			request.getSession().setAttribute("colId", colId);
//		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	public UrlService getUrlService() {
		return urlService;
	}

	public void setUrlService(UrlService urlService) {
		this.urlService = urlService;
	}

	@Override
	public void destroy() {

	}
}
